package com.zerui.manager.app.devicegroup.executer;

import com.alibaba.cola.catchlog.CatchAndLog;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.zerui.cola.dto.SingleResponse;
import com.zerui.manager.devicegroup.DeviceGroup;
import com.zerui.manager.devicegroup.domainservice.DeviceGroupRepository;
import com.zerui.manager.web.vo.DeviceGroupVO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author wh
 * @create_time 2023-08-23 16:29
 */
@Component
@CatchAndLog
public class DeviceGroupQryCmdExe {

    @Resource
    private DeviceGroupRepository deviceGroupRepository;
    public SingleResponse<DeviceGroupVO> execute() {

        DeviceGroup root = deviceGroupRepository.getNodeByName(DeviceGroup.ROOT_NODE_NAME);
        DeviceGroupVO groupDTO = DeviceGroupVO.builder()
                .orgId(root.getId())
                .orgName(root.getOrgName())
                .deviceGroupVoS(getDeviceGroupBySupId(root.getId()))
                .build();
        return SingleResponse.of(groupDTO);
    }

    private List<DeviceGroupVO> getDeviceGroupBySupId(Long supId) {
        List<DeviceGroup> deviceGroups = deviceGroupRepository.getDeviceGroupBySupId(supId);
        List<DeviceGroupVO> deviceGroupVoList = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(deviceGroups)){
            deviceGroups.forEach(e-> deviceGroupVoList.add(DeviceGroupVO.builder()
                    .orgId(e.getId())
                    .orgName(e.getOrgName())
                    .deviceGroupVoS(new ArrayList<>())
                    .build()));
        }
        return deviceGroupVoList;
    }

}
